﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;

public partial class _Default : System.Web.UI.MasterPage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        InitPage();
        ReadCookie();

        string MaLoaiSanPham = "";        
        if (IsPostBack == true)
            MaLoaiSanPham = Request.Form["__EVENTARGUEMENT"];        
        HienDanhSachLoaiSanPham(MaLoaiSanPham);
    }

    private void InitPage()
    {
        panelLogin.Visible = false;
        panelUserState.Visible = false;
        txtUserName.Attributes.Add("onFocus", "ClearText(this)");
        txtPassword.Attributes.Add("onFocus", "ClearText(this)");
    }

    private void HienDanhSachLoaiSanPham(string MaLoaiSanPham)
    {
        string strCommand = "select MaLoaiSanPham, TenLoaiSanPham from LOAISANPHAM";

        SqlCommand cmd = null;
        try
        {
            cmd = new SqlCommand(strCommand, Helper.ConnectToDataBase());
            repeaterLoaiSanPham.DataSource = cmd.ExecuteReader();
            repeaterLoaiSanPham.DataBind();
            Helper.DisposeCommand(ref cmd);
        }
        catch(Exception ex)
        {
            Helper.DisposeCommand(ref cmd);
        }
    }

    protected void UserLogin(object sender, EventArgs e)
    {
        string userName = txtUserName.Text.Trim();
        string password = txtPassword.Text.Trim();

        string  query = "select MaTaiKhoan,MatKhau,MaLoaiTaiKhoan from TAIKHOAN where TenTaiKhoan='"+userName+"'";
        SqlCommand cmd = new SqlCommand(query,Helper.ConnectToDataBase());
        SqlDataReader reader = cmd.ExecuteReader();
        if (reader.HasRows == true)
        {
            reader.Read();
            string passCompare = reader[1].ToString();
            if (password == passCompare)
            {
                LoginSuccess(reader[0].ToString(), userName,reader[2].ToString());
                Helper.DisposeCommand(ref cmd);
                return;
            }
        }

        Helper.DisposeCommand(ref cmd);
        lbLoginFail.Visible = true;
        txtPassword.Text = "";
        txtUserName.Focus();
    }

    private void LoginSuccess(string userID, string userName,string maloaitaikhoan)
    {
        Response.Cookies["daugia_user"]["userid"] = userID;
        Response.Cookies["daugia_user"]["username"] = userName;
        Response.Cookies["daugia_user"]["mltk"] = maloaitaikhoan;
        Response.Cookies["daugia_user"].Expires = DateTime.Now.AddHours(1);

        lnkUserLogin.Text = userName;
        lbLoginFail.Visible = false;
        lnkUserLogin.PostBackUrl = "~/Default" + maloaitaikhoan + ".aspx";
        panelUserState.Visible = true;
        panelLogin.Visible = false;
    }

    private bool ReadCookie()
    {
        try
        {
            if (Request.Cookies["daugia_user"] != null)
            {
                string username = Request.Cookies["daugia_user"]["username"].Trim();
                string userid = Request.Cookies["daugia_user"]["userid"].Trim();
                string mltk = Request.Cookies["daugia_user"]["mltk"].Trim();
                if (username.Length > 0 && userid.Length > 0)
                {
                    lnkUserLogin.Text = username;
                    lnkUserLogin.PostBackUrl = "~/Default" + mltk + ".aspx";
                    lbUserID.Text = userid;
                    panelUserState.Visible = true;
                    return true;
                }
            }
        }
        catch (Exception ex)
        {
            panelLogin.Visible = true;
            return false;
        }

        panelLogin.Visible = true;
        return false;
    }

    protected void UserLogout(object sender, EventArgs e)
    {
        Response.Cookies["daugia_user"].Expires = DateTime.Now.AddDays(-1);
        lbUserID.Text = "";
        panelLogin.Visible = true;
        panelUserState.Visible = false;
    }

}
